package com.controller;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;

import com.bean.Complex;
import com.bean.Project;
import com.bean.Room;
import com.bean.Unit;
import com.service.ComplexService;
import com.service.ProjectService;
import com.service.RoomService;
import com.service.UnitService;


@Controller
@RequestMapping("project")
public class ProjectAction {
	
	@Autowired
	private ProjectService service;
	@Autowired
	private Complex complex;
	@Autowired
	private ComplexService cservice;
	@Autowired
	private Unit unit;
	@Autowired
	private UnitService unitService;
	@Autowired
	private Room room;
	@Autowired
	private RoomService roomService;
	
	
	//跳转自动添加页面
	@RequestMapping("toAddbetter.do")
	public String toAddbetter(){
		return "project/addbetter";
	}
	
	
	//自动添加
	@RequestMapping("addbetter.do")
	public String addbetter(Project project,Integer complexamount,Integer storey,Integer unitamount,Integer roomamount,Float area,Float roomprice,String typeid) throws ParseException{
		
		
		int projectid;
		if(service.maxProject()==null){
			projectid=1;
		}else{
			projectid = service.maxProject()+1;
		}
		project.setId(projectid);
		service.insert(project);
		
		
		Map<String,Object> complexmap = new HashMap<String,Object>();
		Map<String,Object> unitmap = new HashMap<String,Object>();
		Map<String,Object> roommap = new HashMap<String,Object>();
		List<Complex> complexs = new ArrayList<Complex>();
		List<Unit> units = new ArrayList<Unit>();
		List<Room> rooms = new ArrayList<Room>();
		int complexid;
		if(service.maxComplex()==null){
			complexid=1;
		}else{
			complexid = service.maxComplex()+1;
		}
		int unitid;
		if(service.maxUnit()==null){
			unitid=1;
		}else{
			unitid = service.maxUnit()+1;
		}
		int roomid;
		if(service.maxRoom()==null){
			roomid=1;
		}else{
			roomid = service.maxRoom()+1;
		}
		int j=0;
		long t1 = System.currentTimeMillis();
		for (int i = complexid; i < complexid+complexamount; i++) {
			Complex complexx = new Complex();
			complexx.setProjectid(projectid);
			complexx.setId(i);
			j=j+1;
			complexx.setComplexnumber(j+"#");
			complexx.setStorey(storey.toString());
			complexs.add(complexx);
			
			int m=0;
			int ccc = unitid+1;
			for (int k = ccc; k < ccc+unitamount; k++) {
				Unit unitt = new Unit();
				unitt.setComplexid(i);
				unitt.setId(k);
				m=m+1;
				unitt.setUnitenumber(m+"单元");
				unitt.setRoomcount(roomamount*storey);
				units.add(unitt);
				unitid=k;
				//storey总层
				//roomamount/storey单层房间数
				int floor =1;
				int solo=0;
				int xxx = roomid+1;
				for (int x = xxx; x < xxx+roomamount*storey; x++) {
					solo+=1;
					if(solo>roomamount){
						floor+=1;
						solo=1;
					}
					Room roomm = new Room();
					roomm.setRoomnumber(j+"#"+m+"单元"+floor+0+solo);//房间编号
					roomm.setStorey(floor+"");//房间楼层
					roomm.setId(x);
					roomm.setUnitid(k);
					roomm.setArea(area);
					roomm.setRoomprice(roomprice);
					roomm.setRoomtotal(roomprice*area);
					roomm.setTypeid(typeid);
					roomm.setPurposeid("住宅");
					roomm.setState("未开盘");
					rooms.add(roomm);
					roomid = x;
				}
				unitid = k;
			}
			
		}
		long t2 = System.currentTimeMillis();
		complexmap.put("complexs", complexs);
		unitmap.put("units", units);
		roommap.put("rooms", rooms);
		cservice.inserttotal(complexmap);
		unitService.inserttotal(unitmap);
		roomService.inserttotal(roommap);
		long t3 = System.currentTimeMillis();
		System.out.println(t2-t1);
		System.out.println(t3-t2);
		return "redirect:toList.do";
	}
	
	
	
	
	
	
	//跳转自动添加页面
	@RequestMapping("toAddamount.do")
	public String toAddamount(){
		return "project/addamount";
	}
	
	
	//自动添加
	@RequestMapping("addamount.do")
	public String addamount(Project project,Integer complexamount,Integer storey,Integer unitamount,Integer roomamount,Float area,Float roomprice,String typeid) throws ParseException{
		int projectid;
		if(service.maxProject()==null){
			projectid=1;
		}else{
			projectid = service.maxProject()+1;
		}
		project.setId(projectid);
		service.insert(project);
		
		
		int complexid;
		if(service.maxComplex()==null){
			complexid=1;
		}else{
			complexid = service.maxComplex()+1;
		}
		int j=0;
		for (int i = complexid; i < complexid+complexamount; i++) {
			complex.setProjectid(projectid);
			complex.setId(i);
			j=j+1;
			complex.setComplexnumber(j+"#");
			complex.setStorey(storey.toString());
			cservice.insert(complex);
			
			int unitid;
			if(service.maxUnit()==null){
				unitid=1;
			}else{
				unitid = service.maxUnit()+1;
			}
			int m=0;
			for (int k = unitid; k < unitid+unitamount; k++) {
				unit.setComplexid(i);
				unit.setId(k);
				m=m+1;
				unit.setUnitenumber(m+"单元");
				unit.setRoomcount(roomamount*storey);
				unitService.insert(unit);
				
				int roomid;
				if(service.maxRoom()==null){
					roomid=1;
				}else{
					roomid = service.maxRoom()+1;
				}
				//storey总层
				//roomamount/storey单层房间数
				int floor =1;
				int solo=0;
			for (int x = roomid; x < roomid+roomamount*storey; x++) {
				solo+=1;
				if(solo>roomamount){
					floor+=1;
					solo=1;
				}
				room.setRoomnumber(j+"#"+m+"单元"+floor+0+solo);//房间编号
				room.setStorey(floor+"");//房间楼层
					room.setId(x);
					room.setUnitid(k);
					room.setArea(area);
					room.setRoomprice(roomprice);
					room.setRoomtotal(roomprice*area);
					room.setTypeid(typeid);
					room.setPurposeid("住宅");
					room.setState("未开盘");
							
				    roomService.insert(room);
				}
			}
		}
		return "redirect:toList.do";
	}
	
	
	
	
	//列表页面
		@RequestMapping("toList.do")
		public String toList(String name,Model model){
			Map<String, Object> map = new HashMap<String,Object>();
			if(name != null && "".equals(name.trim())){
				name = null;
			}
			map.put("name", name);
			List<Project> list = service.select(map);
			model.addAttribute("list", list);
			model.addAttribute("name",name);
			
			
			
//			Map<String,Object> map1 = new HashMap<String,Object>();
//			List<Complex> complexs = new ArrayList<Complex>();
//			//int complexid = service.maxComplex()+1;
//	
//			for (int i = 0; i < 10000; i++) {
//				Complex complexx = new Complex();
//				complexx.setId(i+5);
//				complexx.setProjectid(1);
//				complexx.setComplexnumber(i+"楼");
//				complexx.setStorey(10+"");
//				complexs.add(complexx);
//				System.out.println(complexx.getId());
////				if(i%100 ==0){
////					map.put("persons",list);
////					openSession.insert("com.zhuoshi.personMapper.insertBatch",map);
////					list.clear();
//			}
//			map1.put("complexs",complexs);
//				
//			cservice.inserttotal(map1);
			
			
			return"project/list";
		}
		
		
		//跳转添加页面
		@RequestMapping("toAdd.do")
		public String toAdd(){
			return "project/add";
		}
		
		
		//添加
		@RequestMapping("add.do")
		public String add(Project project) throws ParseException{
			service.insert(project);
			return "redirect:toList.do";
		}
		
		@InitBinder  
		public void initBinder(WebDataBinder binder) {  
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");  
		dateFormat.setLenient(false);  
		binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));}
		
		//删除
		@RequestMapping("delete.do")
		public String delete(Integer id){
			service.deleteByPrimaryKey(id);
			return "redirect:toList.do";
		}
		
		
		
		//跳转修改
		@RequestMapping("toUpdate.do")
		public String toUpdate(Integer id,Model model){
			Project project = service.selectByPrimaryKey(id);
			model.addAttribute("project", project);
			return "project/update";
		}
		
		//修改
		@RequestMapping("update.do")
		public String update(Project project){
			service.updateByPrimaryKeySelective(project);
			return "redirect:toList.do";
		}
		
		
		

}
